package com.dtagri.fjwlw;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.dtagri.fjwlw.base.BaseApplication;
import com.dtagri.fjwlw.base.TGson;
import com.dtagri.fjwlw.bean.SmallArea;
import com.dtagri.fjwlw.bean.TjAreaMsg;
import com.dtagri.fjwlw.bean.TjEquipment;
import com.dtagri.fjwlw.bean.TjUsersEntity;
import com.dtagri.fjwlw.bean.UserOperate;
import com.dtagri.fjwlw.cache.DataCleanManager;
import com.dtagri.fjwlw.util.MethodsCompat;
import com.dtagri.fjwlw.util.StringUtils;
import com.dtagri.fjwlw.util.TPreferences;
import com.google.gson.reflect.TypeToken;

/**
 * 定义全局的实体信息
 * @author wxjn
 * 
 */
public class AppContext extends BaseApplication {

	private static final String LAST_REFRESH_TIME = "last_refresh_time.pref";

	public static TjUsersEntity user = new TjUsersEntity();

	public static boolean isLogin() {
		return user.getId() != null;
	}

	@Override
	public void onCreate() {
		super.onCreate();

		initLogin();
		initAreaMsg();
		initUserOperate();
	}

	/***
	 * 记录列表上次刷新时间
	 * 
	 * @return void
	 * @param key
	 * @param value
	 */
	public static void putToLastRefreshTime(String key, String value) {
		SharedPreferences preferences = getPreferences(LAST_REFRESH_TIME);
		Editor editor = preferences.edit();
		editor.putString(key, value);
		apply(editor);
	}

	/***
	 * 获取列表的上次刷新时间
	 * 
	 * @return String
	 * @param key
	 * @return
	 */
	public static String getLastRefreshTime(String key) {
		return getPreferences(LAST_REFRESH_TIME).getString(key,
				StringUtils.getCurTimeStr());
	}

	// =============用户开始============
	public static void saveUserInfo(TjUsersEntity tjUsersEntity) {
		user = tjUsersEntity;
		TPreferences.set("USER", tjUsersEntity);
	}

	private void initLogin() {
		AppContext.user = getLoginUser();
	}

	private static TjUsersEntity getLoginUser() {
		TjUsersEntity readObject = TPreferences
				.get("USER", TjUsersEntity.class);
		if (readObject == null)
			return new TjUsersEntity();
		return readObject;
	}

	/**
	 * 清除登录信息
	 * 
	 * @param user2
	 */
	public static void cleanLoginInfo(TjUsersEntity user2) {
		user2.setId(null);
		user2.setPassword(null);
		user = user2;
		TPreferences.set("USER", user2);
	}

	// =============用户结束============

	// =============分区开始============
	public static List<TjAreaMsg> tjAreaMsgs = new ArrayList<TjAreaMsg>();

	public static void saveAreaMsgInfo(List<TjAreaMsg> tjAreaMsgs) {
		AppContext.tjAreaMsgs = tjAreaMsgs;
		TPreferences.set("AREAMSG", tjAreaMsgs);
	}

	private void initAreaMsg() {
		String string = TPreferences.get("AREAMSG", "");
		List<TjAreaMsg> readObject = new ArrayList<TjAreaMsg>();
		Type type = new TypeToken<List<TjAreaMsg>>() {
		}.getType();
		if (!"".equals(string)) {
			readObject = TGson.gson.fromJson(string, type);
		}
		if (readObject != null) {
			AppContext.tjAreaMsgs = readObject;
		} else {
			AppContext.tjAreaMsgs = new ArrayList<TjAreaMsg>();
		}
	}

	public static void cleanAreaMsgInfo() {
		AppContext.tjAreaMsgs = new ArrayList<TjAreaMsg>();
		TPreferences.set("AREAMSG", AppContext.tjAreaMsgs);
	}

	// =============分区结束============

	// =============用户操作开始============

	public static UserOperate userOperate = new UserOperate();

	public static void saveUserOperateInfo(UserOperate userOperate) {
		AppContext.userOperate = userOperate;
		TPreferences.set("USEROPER", userOperate);
	}

	private void initUserOperate() {
		UserOperate readObject = TPreferences
				.get("USEROPER", UserOperate.class);
		if (readObject != null) {
			AppContext.userOperate = readObject;
		} else {
			AppContext.userOperate = new UserOperate();
		}
	}

	public static void cleanUserOperateInfo() {
		AppContext.userOperate = new UserOperate();
		TPreferences.set("USEROPER", AppContext.userOperate);
	}

	// =============用户操作结束============
	/**
	 * 获得当前的传感器
	 * 
	 * @param spinnerSelected2
	 * @param spinnerSelected1
	 * @param spinnerSelected0
	 * 
	 * @return
	 */
	public static TjEquipment curEquipment() {
		SmallArea res = curSmallArea();
		List<TjEquipment> tjEquipments = res.getTjEquipments();
		if (tjEquipments.size() == 0) {
			return null;
		}
		TjEquipment tjEquipment = tjEquipments.get(userOperate
				.getSpinnerSelected2());
		return tjEquipment;
	}

	/**
	 * 获得当前小区
	 * 
	 * @param spinnerSelected1
	 * @param spinnerSelected0
	 * 
	 * @return
	 */
	public static SmallArea curSmallArea() {
		List<SmallArea> tjAreaMsg = curBigArea().getSmallAreas();
		int i = 0;
		SmallArea res = null;
		for (SmallArea smallArea : tjAreaMsg) {
			if (userOperate.getSpinnerSelected1() == i) {
				res = smallArea;
			}
			i++;
		}
		return res;
	}

	public static TjAreaMsg curBigArea() {
		TjAreaMsg tjAreaMsg = tjAreaMsgs.get(userOperate.getSpinnerSelected0());
		return tjAreaMsg;
	}

	/**
	 * 清除app缓存
	 */
	public static void clearAppCache() {
		DataCleanManager.cleanDatabases(AppContext.context());
		// 清除数据缓存
		DataCleanManager.cleanInternalCache(AppContext.context());
		// 2.2版本才有将应用缓存转移到sd卡的功能
		if (MethodsCompat.isMethodsCompat(android.os.Build.VERSION_CODES.FROYO)) {
			DataCleanManager.cleanCustomCache(MethodsCompat
					.getExternalCacheDir(AppContext.context()));
		}
	}
}
